//
//  VideoApplicationViewController.m
//  MIDI
//
//  Created by 王禹丞 on 15/10/20.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "VideoApplicationViewController.h"
#import "AppConfig.h"
#import "APIAdress.h"
#import "AddContentTableViewCell.h"
#import "AddTitlePageTableViewCell.h"
#import "AddVideoBtnTableViewCell.h"
#import "AddVideoTableViewCell.h"
#import "UIActionSheet+Blocks.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "MBProgressHUD+YC.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "UserDataSingleton.h"
#import "CommFunc.h"

@interface VideoApplicationViewController ()<UITableViewDataSource,UITableViewDelegate,AddTitlePageTableViewCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,AddVideoTableViewCellDelegate,AddContentTableViewCellDelegate,AddVideoBtnTableViewCellDelegate,QXNetWorkToolDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UIImagePickerController * headPicker;
@property (strong, nonatomic) UIImagePickerController * videoPicker;
@property (strong, nonatomic) UIImage * pageTitelImage;
@property (copy, nonatomic) NSString *videoUrl;
@property (strong, nonatomic) UIButton *addImageBtn;
@property (copy, nonatomic) NSString *textFieldContent;
@property (copy, nonatomic) NSString *textFieldContent2;
@property (strong, nonatomic) UIImage *editedImage;
@end

@implementation VideoApplicationViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor =UIColorRGBA(240, 240, 240, 1);
    
    self.tableView.showsVerticalScrollIndicator = FALSE;
    
    self.tableView.showsHorizontalScrollIndicator = FALSE;

    
    //导航栏返回按钮
    self.navigationItem.title = @"视频申请";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;

}
    
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
       
        
        return 204;
        
    }
    
    if (indexPath.row == 1) {
        
 
        
        return 80;
        
    }
    if (indexPath.row == 2) {
        
        
        return 192;
        
    }
    if (indexPath.row == 3) {
 
        return 83;
        
    }
    
    return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        
        AddVideoTableViewCell * cell = [AddVideoTableViewCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }
    
    if (indexPath.row == 1) {
        
        AddTitlePageTableViewCell * cell = [AddTitlePageTableViewCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        if (_pageTitelImage) {
            
            [cell.addBtn setImage:_pageTitelImage forState:UIControlStateNormal];
            
            cell.addLabel.text = @"已添加封面";
            
        }
        
        return cell;
        
    }
    if (indexPath.row == 2) {
        
        AddContentTableViewCell * cell = [AddContentTableViewCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }
    if (indexPath.row == 3) {
        
        AddVideoBtnTableViewCell * cell = [AddVideoBtnTableViewCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }

    return nil;

}

-(void)uploadTitlePage:(AddTitlePageTableViewCell *)cell addImageBtn:(UIButton *)addImageBtn{

    self.addImageBtn = addImageBtn;
    //
    [self upLoadHeadphoto];


}


- (void)uploadVideo:(AddVideoTableViewCell *)cell{
    
    self.videoPicker = [[UIImagePickerController alloc] init];
    
    self.videoPicker.delegate = self;
   
    self.videoPicker.allowsEditing = YES;
    
    self.videoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    self.videoPicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    
  [self.navigationController presentViewController:self.videoPicker animated:YES completion:nil];
}

- (void)upLoadHeadphoto
{
    self.headPicker = [[UIImagePickerController alloc] init];
    self.headPicker.mediaTypes = @[(NSString *)kUTTypeImage];
    self.headPicker.allowsEditing = YES;
    self.headPicker.delegate = self;
    
    if (/* DISABLES CODE */ (YES)/*[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]*/) {
        WEAKSELF
        [UIActionSheet showInView:self.view withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"拍照", @"从相册选择"] tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
            switch (buttonIndex) {
                case 0:
                    weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
            if (buttonIndex != actionSheet.cancelButtonIndex) {
                [weakSelf.navigationController presentViewController:weakSelf.headPicker animated:YES completion:nil];
            }
        }];
    }
    else
    {
        self.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.navigationController presentViewController:self.headPicker animated:YES completion:nil];
    }
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if (picker == self.headPicker)
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            
            self.editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
            
            self.pageTitelImage = self.editedImage;
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }];
    }
    
    if (picker == self.videoPicker)
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            
            NSLog(@"%@",info);
            
//            [self movToMp4:[NSURL URLWithString:info[UIImagePickerControllerMediaURL]]];
            
            [MBProgressHUD showSuccess:@"成功拉"];
            
        }];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//第一行cell的textView属性
- (void)uploadVideoUrl:(NSString *)videoUrl{
    
    self.videoUrl = videoUrl;
}

//第三行cell的textField属性
- (void)uploadAddContent:(NSString *)textFieldContent textFieldContent2:(NSString *)textFieldContent2{
    
    self.textFieldContent = textFieldContent;
    self.textFieldContent2 = textFieldContent2;
}

//申请按钮上传资料
- (void)uploadAddVideoBtn:(UIButton *)applyBtn{
    
    [self toDownload];
}

- (void)uploadVideo{
    
    if ([self.videoUrl isEqualToString:@""] || !self.videoUrl) {
        [MBProgressHUD showError:@"请输入视频地址"];
        
        return;
    }
    
    if (!self.editedImage) {
        [MBProgressHUD showError:@"请添加图片封面"];
        
        return;
    }
    
    if ([self.textFieldContent isEqualToString:@""] || !self.textFieldContent) {
        [MBProgressHUD showError:@"请填写视频名称"];
        
        return;
    }
    
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:VideoApplicationInterface andParameter:@[@{@"user_id":[UserDataSingleton shareInstance].user_id,@"show_cove_url":[CommFunc getImageBase64String:self.editedImage],@"show_name":self.textFieldContent,@"show_reamk":self.textFieldContent2,@"show_content_url":self.videoUrl}] tag:1];
}

/**
 *  上传失败
 */
- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showError:Response];
            break;
    }
}

/**
 *  上传成功
 *
 *  @param message 是服务器返回上传成功的一个值
 *  @param tag     对应是哪个接口参数上传的标记
 */
- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    
    [MBProgressHUD hideHUD];
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:message];
            
            break;
    }
}

//提示框 是否确定上传资料？
- (void)toDownload{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"视频申请" message:@"是否确定上传资料" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
    
    [alert addButtonWithTitle:@"确定"];
    
    [alert show];
    
}

//alertView 的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        
        [self uploadVideo];
        
    }
}

@end









